<?php
class Default_ErrorController extends Zend_Controller_Action {

    /**
     * Przechywytywanie błędów
     */
    public function errorAction() {
    	/* @var $config Zend_Config */
    	$config = $this->_helper->config();
        $errors = $this->_getParam('error_handler');
        /* @var $exception Exception */
        $exception = $errors->exception;

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
            	$config = $config->{$errors->type};
                break;
            default:
            	$config = $config->{Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER};
        }

        if (Zend_Registry::isRegistered('Zend_Log')) {
        	$message = sprintf('%s (file:%s [%d]) \n %s',
        					$exception->getMessage(),
        					$exception->getFile(),
        					$exception->getLine(),
        					$exception->getTraceAsString());

        	/* @var $log Zend_Log */
        	$log = Zend_Registry::get('Zend_Log');
	        $log->log(sprintf('Request URI: %s', getenv('REQUEST_URI')), Zend_Log::DEBUG);
	        $log->log(sprintf('HTTP Referer: %s', getenv('HTTP_REFERER')), Zend_Log::DEBUG);
	        $log->log(sprintf('Request IP: %s', KontorX_Util_Functions::getIP()), Zend_Log::DEBUG);
        	$log->log($message, Zend_Log::CRIT);

        }

        /* @var $bootstrap Bootstrap */
		$bootstrap = $this->getInvokeArg('bootstrap');
		/* @var $resource Promotor_Application_Resource_Router */
		$resource = $bootstrap->getPluginResource('Router');

		// dzieki temu dodawany jest przedrostek locale
		$params = $resource->getGlobalParams();
		if (isset($config->params))
		{
			$params = array_merge($params, $config->params->toArray());
		}
		
		$this->_forward($config->action,
						$config->controller,
						$config->module,
						$params);
    }
}